//==============================================================================
// Routines to interpret the data sent from wifiRemote   
//==============================================================================  
#include "MP.h"

//==============================================================================
// Routine to extract the tags from the current message and interpret them
//==============================================================================
extern void __stdcall MP_WififRemote_Decode_Message( char *type, char *line)
{
     int ptr,lptr,linelen,i,j,k,l;
     char str[MAX_STRING],str2[MAX_STRING];
     char authresponse[MAX_STRING];
     int volume,muted;
     TCHAR  system_name[256];
     DWORD  bufCharCount = 256;
     int tr,tl;

     i=0;
     // welcome message received
     // Example: {"Type":"welcome","Server_Version":11,"AuthMethod":0,"MPExtendedServicesInstalled":
     //                                                          {"MAS":false,"TAS":false,"WSS":false}}

     if (!strncmp(type,"welcome",7)) 
       {  // Get and display the name of the computer. 
          bufCharCount = 256;
          if( !GetComputerName( system_name, &bufCharCount ) ) strcpy(system_name,"Computer Name not Known");
          i=strfindstr(line,"AuthMethod\":",12); // returns pointer to start of this string
          if (line[i+12]=='0') // No username/password or pincode required
            { strcpy(authresponse,"{\"Type\":\"identify\",\"Name\":\"");
              strcat(authresponse,system_name);
              strcat(authresponse,"\",\"Application\":\"LCDSmartie MP Client\",\"Version\":\"");
              strcat(authresponse,VER_STRING);
              strcat(authresponse,"\",\"AutologinKey\":null,\"Authenticate\":null}\r\n\r\n"); }
          else if (line[i+12]='1') // send username/password
            { strcpy(authresponse,"{\"Type\":\"identify\",\"Name\":\"");
              strcat(authresponse,system_name);
              strcat(authresponse,"\",\"Application\":\"LCDSmartie MP Client\",\"Version\":\"");
              strcat(authresponse,VER_STRING);
              strcat(authresponse,"\",\"AutologinKey\":null,\"Authenticate\"{ \"AuthMethod\":\"userpass\",\"Password\":\"");
              strcat(authresponse,MP_Global_wPassword);
              strcat(authresponse,"\"User\":\"");
              strcat(authresponse,MP_Global_wUserName);
              strcat(authresponse,"\"}\r\n\r\n"); }
          else if (line[i+12]='2') // send Passcode
            { strcpy(authresponse,"{\"Type\":\"identify\",\"Name\":\"");
              strcat(authresponse,system_name);
              strcat(authresponse,"\",\"Application\":\"LCDSmartie MP Client\",\"Version\":\"");
              strcat(authresponse,VER_STRING);
              strcat(authresponse,"\",\"AutologinKey\":null,\"Authenticate\"{ \"AuthMethod\":\"passcode\",\"Passcode\":\"");
              strcat(authresponse,MP_Global_wPasscode);
              strcat(authresponse,"\"}\r\n\r\n"); }
          else if (line[i+12]='3') // send username/password/passcode
            { strcpy(authresponse,"{\"Type\":\"identify\",\"Name\":\"");
              strcat(authresponse,system_name);
              strcat(authresponse,"\",\"Application\":\"LCDSmartie MP Client\",\"Version\":\"");
              strcat(authresponse,VER_STRING);
              strcat(authresponse,"\",\"AutologinKey\":null,\"Authenticate\"{ \"AuthMethod\":\"userpass\",\"Password\":\"");
              strcat(authresponse,MP_Global_wPassword);
              strcat(authresponse,"\"User\":\"");
              strcat(authresponse,MP_Global_wUserName);
              strcat(authresponse,"\"passcode\":\"");
              strcat(authresponse,MP_Global_wPasscode);
              strcat(authresponse,"\"}\r\n\r\n"); }
          i=MP_Send(WIFIREMOTE,authresponse);
       }
     else if (!strncmp(type,"authenticationresponse",22))
       {  MP_Send(WIFIREMOTE,MP_Global_properties1);    // send out a properties message to subscribe to changes
          if (strlen(MP_Global_properties1)>MAX_PROPS_LENGTH) MP_Send(WIFIREMOTE,MP_Global_properties2);
          MP_Global_authorised=1;            // Note that we are authorised now
       }
     else if (!strncmp(type,"volume",6))
       {  i=strfindstr(line,"Volume\":",8);    // returns pointer to start of this string
          sscanf(&line[i+8],"%d",&volume); sprintf(str,"Volume now %d",volume); 
          i=strfindstr(line,"IsMuted\":",9); // returns pointer to start of this string
          if (line[i+9]=='f') muted=0; else muted=1; 
          MP_Global_prev_start_time=(double)clock()/CLOCKS_PER_SEC;       // Counter start time
          MP_Global_prev=1;
          sprintf(MP_Global_prev_line1,"Volume %d",volume);
          if (muted==1) strcpy(MP_Global_prev_line2,"Muted"); else strcpy(MP_Global_prev_line2,"Not Muted");
       }   
}


//==============================================================================
// Routine to extract the message type from the current message 
// and call decode_message to interpret it
//==============================================================================
extern void __stdcall MP_WifiRemote_Decode(char *mod, char *msgbuf)
{  int ptr,lptr,linelen,i;
   char line[MAX_BUFLEN],str[MAX_STRING],type[MAX_STRING];

   ptr=0; lptr=0;
   linelen=MP_extract_line(msgbuf,0,line);
   while (linelen>0)
    {
      MP_log(mod,line);
      MP_Parse(mod,line);
      
      // decode the message type {"Type":"
      i=strfind(line,'{',0); 
      if (i>0) 
       { ptr=strfind(line,'"',i); 
         if (ptr>0) { if (!strncmp(&line[ptr],"Type",4)) 
                       { i=strfind(line,':',ptr); 
                         if (i>0) { ptr=strfind(line,'"',i); 
                                    if (ptr>0) { i=strfind(line,'"',ptr); 
                                                 strncpy(type,&line[ptr],i-ptr-1); type[i-ptr-1]=0;
                                                 MP_WififRemote_Decode_Message(type,&line[i]);
                                               }
                                  }
                       }
                      else MP_errlog("**** Unhandled","Message does not contain type");
                   }
       }
      lptr=lptr+linelen; // Doesn't compensate for \r \n
      while ( (msgbuf[lptr]=='\r') || (msgbuf[lptr]=='\n') ) lptr=lptr+1;
      linelen=MP_extract_line(&msgbuf[lptr],0,line);     
    }
}

